package com.des1gn.processor;

import com.des1gn.common.bean.RetryErrorMqMessageDto;
import com.rrtv.rpc.client.config.RpcClientProperties;
import com.rrtv.rpc.client.proxy.ClientStubProxyFactory;
import com.rrtv.rpc.core.discovery.DiscoveryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName RetryFaultProcessor
 * @Description 重试故障处理器
 * @Author Des1gn
 * @Version 1.0
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class RetryFaultProcessor {

    private final ClientStubProxyFactory clientStubProxyFactory;

    private final DiscoveryService discoveryService;

    private final RpcClientProperties properties;

    public Boolean invokeRetryFaultTask(RetryErrorMqMessageDto retryErrorMqMessageDto){
        try {
            Class<?> targetClass = Class.forName(retryErrorMqMessageDto.getTargetClassName());
            Method method = targetClass.getMethod(retryErrorMqMessageDto.getTargetMethodName(), retryErrorMqMessageDto.getTargetParamterClass());
            Object proxy = clientStubProxyFactory.getProxy(targetClass, retryErrorMqMessageDto.getTargetVersion(), discoveryService, properties);
            method.invoke(proxy,retryErrorMqMessageDto.getTargetArgs());
            return true;
        } catch (Exception e) {
            log.error("retry fault invoke error -> {}",e.getMessage());
        }
        return false;
    }


}
